package com.ctrl.riveradministrator.mvp.view.activity.trace.daemon;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class DaemonReceiver {

    private ScreenStateListener mScreenStateListener;
    private Context mContext;
    private Receiver receiver;


    public DaemonReceiver(Context context) {
        this.mContext = context;
        receiver = new Receiver();
    }

    public void begin(ScreenStateListener listener) {
        this.mScreenStateListener = listener;
        //屏幕亮灭屏广播
        registerReceiver();
    }

    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mContext.registerReceiver(receiver, filter);
    }

    public void unregisterReceiver(){
        mContext.unregisterReceiver(receiver);
    }


    /**
     * 应用屏幕状态监听
     * 如果屏幕为息屏，则调起1像素activity
     */
    public interface ScreenStateListener {
        void onScreenOn();

        void onScreenOff();
    }


    public class Receiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏
                mScreenStateListener.onScreenOn();
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏
                mScreenStateListener.onScreenOff();
            }
        }
    }
}
